﻿Imports System.Collections.ObjectModel

Imports IntoNails.DataAccess
Imports IntoNails.Models

Namespace ViewModel
    Public Class NailCategoryViewModel
        Inherits TreeNodeViewModel(Of NailCategory)

#Region "Fields"

        Public Shared ReadOnly MockRootCategory As New NailCategoryViewModel(Nothing, Nothing, Nothing)

        Private ReadOnly _repository As NailCategoryRepository
        Private _commands As ReadOnlyCollection(Of ContextMenuCommandViewModel)

#End Region

#Region "Constructors"

        Public Sub New(ByVal nailCategory As NailCategory, ByVal parentCategory As NailCategoryViewModel, ByVal repository As NailCategoryRepository)
            MyBase.New(nailCategory, parentCategory, True)

            _repository = repository
            Me.NodeType = My.Resources.StringResources.NAILCATEGORY_NODETYPE
        End Sub

#End Region

#Region "Properties"

        ''' <summary>
        ''' Gets binding-data's unique code.
        ''' </summary>
        Public ReadOnly Property UniqueCode() As Integer
            Get
                Return Me.BindingData.UniqueCode
            End Get
        End Property

        ''' <summary>
        ''' Gets/sets binding-data's name with UI notifying.
        ''' </summary>
        Public Property CategoryName As String
            Get
                Return Me.BindingData.Name
            End Get
            Set(ByVal value As String)
                If Me.BindingData.Name <> value Then
                    Me.BindingData.Name = value
                    Me.OnPropertyChanged("DisplayName")
                End If
            End Set
        End Property

        ''' <summary>
        ''' Gets/sets binding-data's valid with UI notifying.
        ''' </summary>
        Public Property IsValid() As Boolean
            Get
                Return Me.BindingData.IsValid
            End Get
            Set(ByVal value As Boolean)
                If Me.BindingData.IsValid <> value Then
                    Me.BindingData.IsValid = value
                    Me.OnPropertyChanged("IsValid")
                End If
            End Set
        End Property

        ''' <summary>
        ''' Gets/sets binding-data's date-created with UI notifying.
        ''' </summary>
        Public Property DateCreated() As Date
            Get
                Return Me.BindingData.DateCreated
            End Get
            Set(ByVal value As Date)
                If Me.BindingData.DateCreated <> value Then
                    Me.BindingData.DateCreated = value
                    Me.OnPropertyChanged("DateCreated")
                End If
            End Set
        End Property

        ''' <summary>
        ''' Gets/sets binding-data's date-changed with UI notifying.
        ''' </summary>
        Public Property DateChanged() As Date
            Get
                Return Me.BindingData.DateChanged
            End Get
            Set(ByVal value As Date)
                If Me.BindingData.DateChanged <> value Then
                    Me.BindingData.DateChanged = value
                    OnPropertyChanged("DateChanged")
                End If
            End Set
        End Property

#End Region

#Region "Commands"

        Public ReadOnly Property ContextMenuCommands As ReadOnlyCollection(Of ContextMenuCommandViewModel)
            Get
                If Me._commands Is Nothing Then
                    Dim commands As IList(Of ContextMenuCommandViewModel) = Me.CreateCommands()
                    Me._commands = New ReadOnlyCollection(Of ContextMenuCommandViewModel)(commands)
                End If

                Return Me._commands
            End Get
        End Property

        ''' <summary>
        ''' Creates commands for the node
        ''' </summary>
        Private Function CreateCommands() As IList(Of ContextMenuCommandViewModel)
            Dim commands As New List(Of ContextMenuCommandViewModel)

            ' add sub-menu 'new category' to dummy menu.
            commands.Add(New ContextMenuCommandViewModel(My.Resources.StringResources.NailCategory_Action_AddNew, _
                                                         My.Resources.ImageResources.Category_Add, _
                                                         New SimpleCommand(AddressOf Me.ExecuteAddNode)))

            ' trigger the separator...
            commands.Add(Nothing)

            commands.Add(New ContextMenuCommandViewModel(My.Resources.StringResources.NailCategory_Action_Rename, _
                                                         My.Resources.ImageResources.Category_Rename, _
                                                         New SimpleCommand(AddressOf Me.ExecuteRename)))
            commands.Add(New ContextMenuCommandViewModel(My.Resources.StringResources.NailCategory_Action_Delete, _
                                                         My.Resources.ImageResources.Category_Remove, _
                                                         New SimpleCommand(AddressOf Me.ExecuteDelete)))

            Return commands
        End Function

#End Region

#Region "TreeNodeViewModel Command Actions"

        Protected Overrides Sub ExecuteRename()

        End Sub

        Protected Overrides Sub ExecuteDelete()

        End Sub

        Protected Overrides Sub ExecuteAddNode()

        End Sub

#End Region

#Region "TreeNodeViewModel Overridens"

        Protected Overrides Sub LoadChildren()
            Dim subCategoryVM As NailCategoryViewModel = Nothing

            Dim categoryList As List(Of NailCategory) = Me._repository.GetRawCollection()
            For Each category In categoryList
                subCategoryVM = New NailCategoryViewModel(category, Me, Me._repository)
                AddHandler subCategoryVM.ChildNodeAdded, AddressOf Me.SubCategory_OnChildrenAdded
                AddHandler subCategoryVM.ChildNodeRenamed, AddressOf Me.SubCategory_OnRenamed
                AddHandler subCategoryVM.ChildNodeRemoved, AddressOf Me.SubCategory_OnRemoved
            Next
        End Sub

#End Region

#Region "Sub-Category Event Handlers"

        Private Sub SubCategory_OnRenamed(ByVal sender As Object, ByVal e As TreeNodeEventArgs(Of NailCategory))
            Dim renamedCategory As NailCategoryViewModel = DirectCast(sender, NailCategoryViewModel)
            Dim parent As NailCategoryViewModel = DirectCast(renamedCategory.Parent, NailCategoryViewModel)

            Dim lastIndex As Integer = parent.Children.IndexOf(renamedCategory)

            parent.Children.RemoveAt(lastIndex)
            parent.Children.Insert(lastIndex, renamedCategory)
        End Sub

        Private Sub SubCategory_OnRemoved(ByVal sender As Object, ByVal e As TreeNodeEventArgs(Of NailCategory))
            Dim removedCategory As NailCategoryViewModel = DirectCast(sender, NailCategoryViewModel)
            Dim parent As NailCategoryViewModel = DirectCast(removedCategory.Parent, NailCategoryViewModel)
            If parent IsNot Nothing Then
                parent.Children.Remove(removedCategory)
            End If
        End Sub

        Private Sub SubCategory_OnChildrenAdded(ByVal sender As Object, ByVal e As TreeNodeEventArgs(Of NailCategory))
            Dim parent As NailCategoryViewModel = DirectCast(sender, NailCategoryViewModel)
            If parent.HasMockChild Then
                parent.Children.Remove(MockChild)
            End If

            Dim subCategoryVM As New NailCategoryViewModel(e.Item, parent, Me._repository)
            AddHandler subCategoryVM.ChildNodeAdded, AddressOf Me.SubCategory_OnChildrenAdded
            AddHandler subCategoryVM.ChildNodeRenamed, AddressOf Me.SubCategory_OnRenamed
            AddHandler subCategoryVM.ChildNodeRemoved, AddressOf Me.SubCategory_OnRemoved

            parent.Children.Add(subCategoryVM)
        End Sub

#End Region

    End Class
End Namespace
